using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using GetStartedApp.Pages.Layouts;
using GetStartedApp.ViewModel;
using GetStartedApp.ViewModel.Page;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace GetStartedApp;

public sealed class App : Application
{
    public override void Initialize()
    {
        //Services = ConfigureServices();

        AvaloniaXamlLoader.Load(this);
    }

    // /// <summary>
    // /// Gets the current <see cref="App"/> instance in use
    // /// </summary>
    // public static new App Current => (App)Application.Current;
    //
    // /// <summary>
    // /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
    // /// </summary>
    // public IServiceProvider Services { get; private set; }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow
                                 {
                                     DataContext = new MainWindowViewModel()

                                     //Current.Services.GetRequiredService<MainWindowViewModel>() };
                                 };
        }

        base.OnFrameworkInitializationCompleted();
    }

    /// <summary>
    /// Configures the services for the application.
    /// </summary>
    private static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();

        // Services
        // .AddSingleton<IEmailService, EmailService>();

        // ViewModel
        services.AddTransient<IndexViewModel>();
        services.AddTransient<BindPageViewModel>();
        services.AddTransient<CommandPageViewModel>();
        services.AddTransient<ControlViewModel>();
        services.AddTransient<DataGridPageViewModel>();
        services.AddTransient<ItemsPanelPageViewModel>();
        services.AddTransient<SelectedFilesViewModel>();
        services.AddTransient<TabControlPageModel>();
        services.AddTransient<TreeViewPageViewModel>();
        services.AddTransient<TriggerPageModel>();
        services.AddTransient<MainWindowViewModel>();

        services.AddTransient<GridFormPageView>();

        var serviceProvider = services.BuildServiceProvider();

        // Ioc.Default.ConfigureServices(serviceProvider);
        //Ioc.Default.GetService<ILogger>().Log("Hello world!");

        return serviceProvider;
    }
}